home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / TERMLIB / TGETENT.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  13KB  |  467 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18. /*
  19.  * Modified:
  20.  *    30-Apr-86 Mic Kaczmarczik ...!ihnp4!seismo!ut-sally!ut-ngp!mic
  21.  *        Instead of using VAX C getenv("TERM"), which does not
  22.  *        return the value of logical name "TERM", translate the
  23.  *        logical name by hand.
  24.  *    11-Oct-86 Mic Kaczmarczik ...!ihnp4!seismo!ut-sally!ut-ngp!mic
  25.  *        Support tc capability to allow the library to use standard
  26.  *        termcaps.  Rewrote tgetent to look for tc capability
  27.  *        and add new terminal definition to the caller's buffer.
  28.  *        This makes it rather possible to overflow the caller's
  29.  *        buffer, but the library doesn't make any claim that it
  30.  *        won't overwrite the buffer anyway...
  31.  *    27-Jan-88 Bob Larson  blarson@ecla.usc.edu
  32.  *        Add primos (__50SERIES) support.
  33.  *    1-Feb-88  Sandra Loosemore  (sandra@cs.utah.edu)
  34.  *          Change default termcap file for VMS to be the same place
  35.  *          that GNU puts it.
  36.  *    23-Apr-88 Bob Larson
  37.  *          merge primos and vms changes
  38.  */
  39.  
  40.  
  41.  
  42. /*
  43.  *  LIBRARY FUNCTION
  44.  *
  45.  *    tgetent    load buffer with entry for specified terminal
  46.  *
  47.  *  KEY WORDS
  48.  *
  49.  *    termcap functions
  50.  *    utility routines
  51.  *
  52.  *  SYNOPSIS
  53.  *
  54.  *    int tgetent(bp,name)
  55.  *    char *bp;
  56.  *    char *name;
  57.  *
  58.  *  DESCRIPTION
  59.  *
  60.  *    Extracts the entry for terminal <name> from the termcap file
  61.  *    and places it in the character buffer <bp>.   It is currently
  62.  *    assumed that bp is at least 1024 characters.  If the entry in
  63.  *    the termcap file is larger than 1023 characters the excess
  64.  *    characters will be discarded and appropriate status will
  65.  *    be returned.
  66.  *
  67.  *    Also note that since bp is used by other termcap
  68.  *    routines, the storage associated with the termcap entry
  69.  *    cannot be freed until all termcap calls are completed.
  70.  *
  71.  *    Tgetent can be directed to look in a file other than
  72.  *    the default (/etc/termcap) by defining an environment
  73.  *    variable called TERMCAP to be the pathname of the desired
  74.  *    termcap file.  This is useful for debugging new entries.
  75.  *    NOTE: the pathname MUST begin with a '/' character.
  76.  *
  77.  *    Also, if the string assigned to TERMCAP does not begin with
  78.  *    a '/' and if the environment variable TERM matches <name> then
  79.  *    the string assigned to TERMCAP is copied to buffer <bp>
  80.  *    instead of reading a termcap file.
  81.  *
  82.  *  RETURNS
  83.  *
  84.  *    -1  if the termcap file cannot be opened
  85.  *     0  if no entry in termcap file matches <name>
  86.  *     1  if extraction is successful with no errors
  87.  *     2  if extraction is successful but entry truncated
  88.  *
  89.  *  SEE ALSO
  90.  *
  91.  *    tgetnum    extract numeric type capability
  92.  *    tgetflag    test boolean type capability
  93.  *    tgetstr    get string value of capability
  94.  *
  95.  *  AUTHOR
  96.  *
  97.  *    Fred Fish
  98.  *
  99.  */
  100.  
  101. #include <stdio.h>
  102.  
  103. #define TRUE 1
  104. #define FALSE 0
  105. #define BUFSIZE 1024              /* Assumed size of external buffer */
  106.  
  107. #define NO_FILE           -1              /* Returned if can't open file */
  108. #define NO_ENTRY  0              /* Returned if can't find entry */
  109. #define SUCCESS      1              /* Returned if entry found ok */
  110. #define TRUNCATED 2              /* Returned if entry found but trunc */
  111.  
  112. #ifdef __50SERIES
  113. #define DEFAULT_FILE "mg*>termcap"
  114. #else
  115. #define DEFAULT_FILE "/etc/termcap"   /* default termcap filename */
  116. #ifdef          VAXC
  117. #define          index strchr
  118. #endif
  119. #endif
  120.  
  121. char *_tcpbuf;                  /* Place to remember buffer pointer */
  122. FILE *fp;                  /* Termcap file              */
  123. static FILE *find_file();
  124. extern char *index();
  125.  
  126. /*
  127.  *  PSEUDO CODE
  128.  *
  129.  *    Begin tgetent
  130.  *      Erase any previous buffer contents.
  131.  *      Remember the buffer pointer.
  132.  *      If termcap file is not found then
  133.  *          If buffer was filled anyway then
  134.  *          Return SUCCESS.
  135.  *          Else
  136.  *          Return NO_FILE.
  137.  *          End if
  138.  *      Else
  139.  *          Find entry associated with name
  140.  *          While an entry was found and limit not reached
  141.  *          If no tc capability found Then
  142.  *              Exit while loop with status = SUCCESS
  143.  *          Else
  144.  *              Call getent to get entry indicated by tc=
  145.  *              If entry not found then
  146.  *                  Exit loop with status != SUCCESS
  147.  *              End if
  148.  *              Concatenate entry into buffer
  149.  *          End If
  150.  *          End while
  151.  *      End if
  152.  *      Close termcap file
  153.  *      Return status code
  154.  *    End tgetent
  155.  *
  156.  */
  157.  
  158. static int getent();
  159.  
  160. int tgetent(bp,name)
  161. char *bp;
  162. char *name;
  163. {
  164.       char    *tc, *tcbufp, tcbuf[80], termbuf[BUFSIZE], *tgetstr();
  165.       char    *bufp, *cp;          /* current start of buffer      */
  166.       int     limit = 10;          /* maximum nesting          */
  167.       int     status;              /* return from getent()          */
  168.  
  169.       *bp = '\0';              /* clear buffer              */
  170.       _tcpbuf = bp;              /* save base of buffer          */
  171.  
  172.       /* Look for termcap file.     If NULL, find_file may have found a  */
  173.       /* a valid termcap string in the environment variable TERMCAP.  */
  174.       /* If non-null, attempt to find the entry in the termcap file   */
  175.  
  176.       if ((fp = find_file(bp)) == NULL) {
  177.           if (*bp != NULL)
  178.               return(SUCCESS);
  179.           else
  180.               return(NO_FILE);
  181.       }
  182.       status = getent(bp, name);/* look for main entry          */
  183.  
  184.       /* Start looking for tc capabilities in the termcap.  If
  185.        * found, concatenate the entry for the new terminal to the
  186.        * current buffer and try again.    To avoid infinite loops,
  187.        * allow only 10 loops through this process.
  188.        */
  189.       while ((status == SUCCESS) && limit--) {
  190.           /* look for tc capability.  If none found, exit loop    */
  191.           tcbufp = tcbuf;
  192.           if (((tc = tgetstr("tc",&tcbufp)) == NULL)
  193.             || (*tc == '\0')) {
  194.               status = SUCCESS;/* no more tc= entries */
  195.               break;
  196.           }
  197.  
  198.           /* Attempt to get next entry. Exit loop if unsuccessful */
  199.           if ((status = getent(termbuf, tcbuf)) != SUCCESS)
  200.               break;
  201.  
  202.           /* Copy new entry into buffer, right at "tc="          */
  203.           for (bufp = bp; *bufp; bufp++)          /* find tc=     */
  204.               if ((*bufp=='t') && (bufp[1]=='c') && (bufp[2]=='='))
  205.                   break;
  206.           if ((cp = index(termbuf,':')) == NULL)
  207.               cp = termbuf;
  208.           strcpy(bufp, cp + 1);
  209.       }
  210.  
  211.       /* close termcap file and return the status     */
  212.       fclose(fp);
  213.       return status;
  214. }
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  *  INTERNAL FUNCTION
  221.  *
  222.  *    getent    find termcap entry in termcap file
  223.  *
  224.  *  KEY WORDS
  225.  *
  226.  *    internal functions
  227.  *    getent
  228.  *
  229.  *  SYNOPSIS
  230.  *
  231.  *    static int getent(bp,name)
  232.  *    char *bp;
  233.  *    char *name;
  234.  *
  235.  *  DESCRIPTION
  236.  *
  237.  *    Getent is called by tgetent each time tgetent attempts to
  238.  *    read a capability from the termcap database file.     Places
  239.  *    the entry in the buffer pointed to by bp
  240.  *
  241.  *
  242.  *  PSEUDOCODE
  243.  *
  244.  *    Begin Getent
  245.  *      Seek to beginning of termcap file
  246.  *      Clear buffer
  247.  *      While records left to process
  248.  *          If this is entry is what we want then
  249.  *          If entry was truncated then
  250.  *              Return TRUNCATED status
  251.  *          Else
  252.  *              Return SUCCESS status.
  253.  *          End if
  254.  *          End if
  255.  *      End while
  256.  *      Return NO_ENTRY status.
  257.  *    End
  258.  */
  259.  
  260. static int getent(bp,name)
  261. char *bp;                  /* Pointer to buffer (1024 char min) */
  262. char *name;                  /*